home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / KWIC.ICN < prev    next >
Text File  |  1992-11-20  |  3KB  |  95 lines

  1. ############################################################################
  2. #
  3. #    File:     kwic.icn
  4. #
  5. #    Subject:  Program to produce keywords in context
  6. #
  7. #    Author:   Stephen B. Wampler, modified by Ralph E. Griswold
  8. #
  9. #    Date:     September 14, 1992
  10. #
  11. ###########################################################################
  12. #
  13. #     This is a simple keyword-in-context (KWIC) program. It reads from
  14. #  standard input and writes to standard output. The "key" words are
  15. #  aligned in column 40, with the text shifted as necessary. Text shifted
  16. #  left is truncated at the left. Tabs and other characters whose "print width"
  17. #  is less than one may not be handled properly.
  18. #
  19. #     If an integer is given on the command line, it overrides the default
  20. #  40.
  21. #
  22. #     Some noise words are omitted (see "exceptions" in the program text).
  23. #  If a file named except.wrd is open and readable in the current directory,
  24. #  the words in it are used instead.
  25. #
  26. #     This program is pretty simple.  Possible extensions include ways
  27. #  of specifying words to be omitted, more flexible output formatting, and
  28. #  so on.  Another "embellisher's delight".
  29. #
  30. ############################################################################
  31.  
  32. global line, loc, exceptions, width
  33.  
  34. procedure main(args)
  35.    local exceptfile
  36.  
  37.    width := integer(args[1]) | 40
  38.  
  39.    if exceptfile := open("except.wrd") then {
  40.       exceptions := set()
  41.       every insert(exceptions, lcword(exceptfile))
  42.       close(exceptfile)
  43.       }
  44.    else
  45.       exceptions := set(["or", "in", "the", "to", "of", "on", "a",
  46.          "an", "at", "and", "i", "it", "by", "for"])
  47.  
  48.    every write(kwic(&input))
  49.  
  50. end
  51.  
  52. procedure kwic(file)
  53.    local index, word
  54.  
  55. #  Each word, in lowercase form, is a key in the table "index".
  56. #  The corresponding values are lists of the positioned lines
  57. #  for that word.  This method may use an impractically large
  58. #  amount of space for large input files.
  59.  
  60.    index := table()
  61.    every word := lcword(file) do {
  62.       if not member(exceptions,word) then {
  63.          /index[word] := []
  64.          index[word] := put(index[word],position())
  65.          }
  66.       }
  67.  
  68. #  Before the new sort options, it was done this way -- the code preserved
  69. #  as an example of "generators in action".
  70.  
  71. #  suspend !((!sort(index,1))[2])
  72.  
  73.    index := sort(index,3)
  74.    while get(index) do
  75.       suspend !get(index)
  76. end
  77.  
  78. procedure lcword(file)
  79.    static chars
  80.    initial chars := &ucase ++ &lcase ++ '\''
  81.    every line := !file do
  82.       line ? while tab(loc := upto(chars)) do
  83.          suspend map(tab(many(chars)) \ 1)
  84. end
  85.  
  86. procedure position()
  87.    local offset
  88.  
  89. #  Note that "line" and ""loc" are global.
  90.  
  91.    offset := width - loc
  92.    if offset >= 0 then return repl(" ",offset) || line
  93.    else return line[-offset + 1:0]
  94. end
  95.